package hell.satan.droidbdf.utils;

import android.app.Application;
import android.content.res.Resources;
import android.os.Environment;

import java.io.File;

import hell.satan.droidbdf.base.BaseApp;

/**
 * 全局常量
 * Global constants
 *
 * @author Cerberus
 */
@SuppressWarnings("unused")
public class Constants {
    /**
     * ID生成器
     * ID Generator
     */
    protected static final Generator GENERATOR = new Generator();

    /**
     * 全局可用的Application对象
     * Global Application instance
     */
    public static final Application APP = BaseApp.getInstance();

    /**
     * 应用通用名称，从配置文件中读取，默认为包名
     * Application common name, read from the config file, the default value is the package name
     */
    public static final String NAME = getConfig("name", APP.getPackageName());

    /**
     * 默认编码名称
     * Default charset name
     */
    public static final String CHARSET = getConfig("charset", "utf-8");

    /*
     * 常见时间常量所对应的毫秒数
     * The common time constants in milliseconds
     */
    public static final long TIME_SECOND = 1000;
    public static final long TIME_MINUTE = 60 * TIME_SECOND;
    public static final long TIME_HOUR = 60 * TIME_MINUTE;
    public static final long TIME_DAY = 24 * TIME_HOUR;
    public static final long TIME_WEEK = 7 * TIME_DAY;
    public static final long TIME_QUARTER = 15 * TIME_MINUTE;

    /*
     * 常见磁盘空间常量所对应的字节数
     * The common size constants in byte
     */
    public static final long SIZE_KB = 1024;
    public static final long SIZE_MB = 1024 * SIZE_KB;
    public static final long SIZE_GB = 1024 * SIZE_MB;
    public static final long SIZE_TB = 1024 * SIZE_GB;
    public static final long SIZE_PB = 1024 * SIZE_TB;

    /*
     * 常用的文件
     * The common files
     */
    public static final File FILE_SDCARD = Environment.getExternalStorageDirectory();
    public static final File FILE_ROOT = new File(FILE_SDCARD, NAME);
    public static final File FILE_CACHE = new File(FILE_ROOT, ".cache");
    public static final File FILE_CRASH = new File(FILE_ROOT, "crash");

    /*
     * 常用的文件路径
     * The common file locations
     */
    public static final String PATH_SDCARD = FILE_SDCARD.getAbsolutePath();
    public static final String PATH_ROOT = FILE_ROOT.getAbsolutePath();
    public static final String PATH_CACHE = FILE_CACHE.getAbsolutePath();
    public static final String PATH_CRASH = FILE_CRASH.getAbsolutePath();

    /*
     * 通用Intent的附加参数
     * The common extra parameters for intent
     */
    public static final String EXTRA_ACTIVITY_FROM = "extra_activity_from";

    public static final String EXTRA_FRAGMENT_URL = "extra_fragment_url";

    /**
     * 获取字符型的配置值
     * Get String config value
     *
     * @param name 配置名称
     *             Config name
     * @param def  默认值
     *             Default value
     * @return 配置值 Config value
     */
    protected static String getConfig(String name, String def) {
        try {
            Resources res = APP.getResources();
            int id = res.getIdentifier(name, "string", APP.getPackageName());
            return res.getString(id);
        } catch (Resources.NotFoundException ex) {
            return def;
        }
    }

    /**
     * 获取布尔型的配置值
     * Get String config value
     *
     * @param name 配置名称
     *             Config name
     * @param def  默认值
     *             Default value
     * @return 配置值 Config value
     */
    protected static boolean getConfig(String name, boolean def) {
        try {
            Resources res = APP.getResources();
            int id = res.getIdentifier(name, "bool", APP.getPackageName());
            return res.getBoolean(id);
        } catch (Resources.NotFoundException ex) {
            return def;
        }
    }

    /**
     * 获取整数型的配置值
     * Get String config value
     *
     * @param name 配置名称
     *             Config name
     * @param def  默认值
     *             Default value
     * @return 配置值 Config value
     */
    protected static int getConfig(String name, int def) {
        try {
            Resources res = APP.getResources();
            int id = res.getIdentifier(name, "integer", APP.getPackageName());
            return res.getInteger(id);
        } catch (Resources.NotFoundException ex) {
            return def;
        }
    }

    static {
        FILE_ROOT.mkdirs();
        FILE_CACHE.mkdirs();
        FILE_CRASH.mkdirs();
    }
}
